package com.streamsets.datacollector.pileline.stage;

import com.streamsets.datacollector.bundles.Constants;
import com.streamsets.datacollector.pileline.enums.HiveMetadataEnum;
import com.streamsets.datacollector.restapi.bean.ConfigConfigurationJson;
import com.streamsets.datacollector.restapi.bean.StageConfigurationJson;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始化HiveMetadata查询配置
 * @author wengzhonghui
 * @date 2019/10/16 19:12
 */
public class InitHiveMetadataConfig {

    /*
     * hive metadata stage名称
     */
    public static final String STAGE_HIVE_QUERY_NAME = "HiveMetadata_01";

    public static void init(StageConfigurationJson stage, String defaultHiveJdbcUrl
            , String targetDb, String targetTable){
        if(targetTable!=null){
            targetTable = targetTable.toLowerCase();
        }
        if(STAGE_HIVE_QUERY_NAME.equalsIgnoreCase(stage.getInstanceName())) {
            if (StringUtils.isNotEmpty(targetTable)) {
                List<ConfigConfigurationJson> configs =
                    intConfig(stage.getConfiguration(), defaultHiveJdbcUrl, targetDb, targetTable);
                stage.setConfiguration(configs);
            }
        }
    }

    public static List<ConfigConfigurationJson> intConfig(List<ConfigConfigurationJson> oldConfigs
            , String defaultHiveJdbcUrl, String targetDb, String targetTable){
        List<ConfigConfigurationJson> newConfigs = new ArrayList<>();
        for(ConfigConfigurationJson config :oldConfigs ){
            config = initHiveJDBCUrl(config, defaultHiveJdbcUrl);
            config = initHiveDriver(config);
            config = initTargetDb(config, targetDb);
            config = initTargetTable(config, targetTable);
            newConfigs.add(config);
        }
        return newConfigs;
    }


    // 初始化hiveJdbc连接
    private static ConfigConfigurationJson initHiveJDBCUrl(ConfigConfigurationJson config, String defaultHiveJdbcUrl){
        if(HiveMetadataEnum.hiveJDBCUrl.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveMetadataEnum.hiveJDBCUrl.getName()
                    ,defaultHiveJdbcUrl);
        }
        return config;
    }

    // 初始化hive驱动
    private static ConfigConfigurationJson initHiveDriver(ConfigConfigurationJson config){
        if(HiveMetadataEnum.hiveJDBCDriver.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveMetadataEnum.hiveJDBCDriver.getName()
                    ,HiveMetadataEnum.hiveJDBCDriver.getDefaultValue());
        }
        return config;
    }

    // 初始化目标数据库
    private static ConfigConfigurationJson initTargetDb(ConfigConfigurationJson config
            , String targetDb){
        if(HiveMetadataEnum.dbNameEL.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveMetadataEnum.dbNameEL.getName()
                    ,targetDb);
        }
        return config;
    }

    // 初始化目标表，一般先放在临时表
    private static ConfigConfigurationJson initTargetTable(ConfigConfigurationJson config
            , String targetTable){
        String tableInc = Constants.PROPERTIES_OLLECT_TEMP_TABLE_PREFIX  + targetTable + "_inc";
        if(HiveMetadataEnum.tableNameEL.getName().equalsIgnoreCase(config.getName())){
            config = new ConfigConfigurationJson(HiveMetadataEnum.tableNameEL.getName()
                    ,tableInc);
        }
        return config;
    }

}
